﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com;
using Com.BU;
using Com.DA;
using ColdStorage.DOL;
using System.Media;
using System.Windows.Controls.Primitives;


namespace ColdStorage.GUI
{
    /// <summary>
    /// Interaction logic for ucConstructionCold.xaml
    /// </summary>
    public partial class ucConstructionCold : UserControl
    {
        int iWall = 1;
        int iRoof = 1;

        public string key = "";

        Wall w;
        Roof r;
        Floor f;
        List<Roof> lroof;
        List<Wall> lwall;

        //Deletgate cho đối tượng wall
        public delegate void GetWallList();
        public GetWallList MyWalllist;

        //Delegate cho đối tượng roof
        public delegate void GetRoofList();
        public GetRoofList MyRoofList;

        //Delegate cho đối tượng Floor
        public delegate void GetFloor(object fFloor);
        public GetFloor MyFloor;

        // Xử lý xóa tường
        public delegate void DeleteWall(object wall);
        public DeleteWall DeleteMyWall;

        // Xử lý xóa mái
        public delegate void DeleteRoof(object roof);
        public DeleteRoof DeleteMyRoof;

        public ucConstructionCold()
        {
            InitializeComponent();
            loadInfo();
        }

        public void loadInfo()
        {
            //Load loại Roof
            cbxRoofType.Items.Clear();
            cbxRoofType.ItemsSource = LibraryManagerInCom.RoofList;
            cbxRoofType.DisplayMemberPath = "ceiling_type";
            cbxRoofType.SelectedIndex = -1;

            // Load loai tuong
            cbxWallType.Items.Clear();
            cbxWallType.ItemsSource = LibraryManagerInCom.wallTypeList;
            cbxWallType.DisplayMemberPath = "wall_type";
            cbxWallType.SelectedIndex = -1;

            //Load vật liệu tường
            cbxWallMaterials.Items.Clear();
            cbxWallMaterials.ItemsSource = LibraryManagerInCom.CSWallMaterialList;
            cbxWallMaterials.DisplayMemberPath = "wall_type";
            cbxWallMaterials.SelectedIndex = -1;

            // Load Hướng
            cbxDirection.Items.Clear();
            cbxDirection.ItemsSource = LibraryManagerInCom.wallDirectionList;
            cbxDirection.DisplayMemberPath = "direction";
            cbxDirection.SelectedIndex = -1;

            EnableWall(false);
            EnableRoof(false);
        }

        #region PROCESS WALL

        private void radYes_Checked(object sender, RoutedEventArgs e)
        {
            cbxDirection.IsEnabled = true;
            txtWallTempExcess.IsEnabled = true;
            lblWallTempExcess.IsEnabled = true;
            lblDirection.IsEnabled = true;
            btnApplyConstruction.IsEnabled = true;
        }

        private void radNo_Checked(object sender, RoutedEventArgs e)
        {
            cbxDirection.SelectedIndex = -1;
            txtWallTempExcess.Text = "0";
            cbxDirection.IsEnabled = false;
            txtWallTempExcess.IsEnabled = false;
            lblWallTempExcess.IsEnabled = false;
            lblDirection.IsEnabled = false;
            btnApplyConstruction.IsEnabled = true;
        }

        private void cbxWallType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxWallType.SelectedItem != null)
            {
                txtWallHeatTransferCoefficient.Text = ((tb_wall_type)cbxWallType.SelectedItem).heat_transfer_coefficient.ToString();
                btnApplyConstruction.IsEnabled = true;
            }
        }

        /// Lay du lieu hieu nhiet do du tu vat lieu tuong va huong
        public void loadTempExcess()
        {
            tb_wall_direction wallDirection = (tb_wall_direction)cbxDirection.SelectedItem;
            tb_wall_material wallMaterial = (tb_wall_material)cbxWallMaterials.SelectedItem;
            if (wallDirection != null && wallMaterial != null)
            {
                tb_temperature_excess te = LibraryManagerInCom.ColdStorageWallList.SingleOrDefault
                    (s => s.wall_material_id == wallMaterial.id && s.direction_id == wallDirection.id);
                if (te != null)
                {
                    txtWallTempExcess.Text = te.termperature_excess.ToString();
                }
            }
        }

        private void cbxDirection_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxDirection.SelectedItem != null)
            {
                loadTempExcess();
                btnApplyConstruction.IsEnabled = true;
            }
        }

        private void cbxWallMaterials_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxWallMaterials.SelectedItem != null)
            {
                loadTempExcess();
                btnApplyConstruction.IsEnabled = true;
            }
        }

        public void AddWall(Wall w)
        {
            bool overload = false;
            if (txtTagWall.Text != "")
            {
                foreach (Wall wal in lwall)
                {
                    if (wal.sWallName == txtTagWall.Text)
                    {
                        overload = true;
                        w.sWallName = "Wall " + iWall;
                        break;
                    }
                }
                if (!overload)
                {
                    w.sWallName = txtTagWall.Text;
                }
            }
            else
            {
                w.sWallName = "Wall " + iWall;
            }
            CheckWallName(w);
            iWall++;
            lwall.Add(w);
            loadlstWall();
            txtTagWall.Clear();
        }

        public void CheckWallName(Wall w)
        {
            foreach (Wall wa in lwall)
            {
                if (w.sWallName == wa.sWallName)
                {
                    iWall++;
                    w.sWallName = "Wall " + iWall;
                    CheckWallName(w);
                }
            }
        }

        private void btnAddWall_Click(object sender, RoutedEventArgs e)
        {
            Wall w = new Wall();
            AddWall(w);
        }

        private void btnDeleteWall_Click(object sender, RoutedEventArgs e)
        {
            lwall.Remove(w);
            loadlstWall();
            txtTagWall.Text = "";
            EnableWall(false);
            if (lwall.Count == 0)
            {
                iWall = 1;
            }
        }


        private void btnCopyWall_Click(object sender, RoutedEventArgs e)
        {
            if (w != null)
            {
                Wall copywall = (Wall)w.Clone();
                AddWall(copywall);
            }
        }

        public void loadlstWall()
        {
            lstWall.ItemsSource = null;
            lstWall.Items.Clear();
            lstWall.ItemsSource = lwall;
            lstWall.DisplayMemberPath = "sWallName";
        }

        private void lstWall_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            EnableWall(true);
            w = (Wall)lstWall.SelectedItem;
            if (w != null)
            {
                txtTagWall.Text = w.sWallName;
                txtWallLength.Text = w.dbWallLength.ToString();
                txtWallWidth.Text = w.dbWallWidth.ToString();

                txtWallOutsideExposure.Text = w.dbWallOutsideExposure.ToString();
                txtWallHeatTransferCoefficient.Text = w.dbWallHeatTransferCoefficient.ToString();
                cbxWallType.SelectedIndex = Convert.ToInt32(w.iWallType);
                cbxWallMaterials.SelectedIndex = Convert.ToInt32(w.iWallMaterials);

                if (w.dbWallTemperatureExcess != 0 || w.iWallDirection != -1)
                {
                    cbxDirection.SelectedIndex = w.iWallDirection;
                    txtWallTempExcess.Text = w.dbWallTemperatureExcess.ToString();
                    radYes.IsChecked = true;
                    cbxDirection.IsEnabled = true;
                    txtWallTempExcess.IsEnabled = true;
                    lblWallTempExcess.IsEnabled = true;
                    lblDirection.IsEnabled = true;
                }
                else
                {
                    radNo.IsChecked = true;
                    cbxDirection.IsEnabled = false;
                    txtWallTempExcess.IsEnabled = false;
                    lblWallTempExcess.IsEnabled = false;
                    lblDirection.IsEnabled = false;
                }
            }
        }

        public void EnableWall(bool b)
        {
            cbxWallType.IsEnabled = b;
            txtWallHeatTransferCoefficient.IsEnabled = b;
            cbxWallMaterials.IsEnabled = b;
            txtWallOutsideExposure.IsEnabled = b;
            radYes.IsEnabled = b;
            radNo.IsEnabled = b;
            cbxDirection.IsEnabled = b;
            txtWallTempExcess.IsEnabled = b;
            txtWallLength.IsEnabled = b; ;
            txtWallWidth.IsEnabled = b;
        }


        private void lstWall_GotFocus(object sender, RoutedEventArgs e)
        {
            SaveWall();
        }

        private void SaveWall()
        {
            if (w != null)
            {
                if (txtTagWall.Text != "")
                {
                    w.sWallName = txtTagWall.Text;
                }
                w.dbWallHeatTransferCoefficient = Convert.ToDouble(txtWallHeatTransferCoefficient.Text);
                if (radYes.IsChecked == true)
                {
                    w.dbWallTemperatureExcess = Convert.ToDouble(txtWallTempExcess.Text);
                    w.iWallDirection = cbxDirection.SelectedIndex;
                }
                else
                {
                    w.dbWallTemperatureExcess = 0;
                    w.iWallDirection = -1;
                }
                w.dbWallOutsideExposure = Convert.ToDouble(txtWallOutsideExposure.Text);
                w.dbWallWidth = Convert.ToDouble(txtWallWidth.Text);
                w.dbWallLength = Convert.ToDouble(txtWallLength.Text);
                w.iWallMaterials = cbxWallMaterials.SelectedIndex;
                w.iWallType = cbxWallType.SelectedIndex;
            }
        }


        public void loadWall(List<Wall> lw)
        {
            lwall = lw;
            loadlstWall();
            if (lwall.Count == 0)
            {
                EnableWall(false);
            }
        }

        #endregion

        #region PROCESS ROOF
        private void cbxRoofType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxRoofType.SelectedItem != null)
            {
                txtRoofTempExcess.Text = ((tb_ceiling_type)cbxRoofType.SelectedItem).temperature_excess.ToString();
                btnApplyConstruction.IsEnabled = true;
            }
        }

        public void AddRoof(Roof r)
        {
            bool overload = false;
            if (txtTagCeiling.Text != "")
            {
                foreach (Roof ro in lroof)
                {
                    if (ro.sRoofName == txtTagCeiling.Text)
                    {
                        r.sRoofName = "Roof " + iRoof;
                        overload = true;
                        break;
                    }
                }
                if (!overload)
                {
                    r.sRoofName = txtTagCeiling.Text;
                }
            }
            if (txtTagCeiling.Text == "")
            {
                r.sRoofName = "Roof " + iRoof;
            }
            CheckRoofName(r);
            iRoof++;
            lroof.Add(r);
            loadlstRoof();
            txtTagCeiling.Clear();
        }

        public void CheckRoofName(Roof r)
        {
            foreach (Roof ro in lroof)
            {
                if (r.sRoofName == ro.sRoofName)
                {
                    iRoof++;
                    r.sRoofName = "Roof " + iRoof;
                    CheckRoofName(r);
                }
            }
        }

        private void btnNewRoof_Click(object sender, RoutedEventArgs e)
        {
            Roof r = new Roof();
            AddRoof(r);
        }

        private void btnCopyRoof_Click(object sender, RoutedEventArgs e)
        {
            if (r != null)
            {
                Roof RoofCopy = (Roof)r.Clone();
                AddRoof(RoofCopy);
            }
        }

        private void btnDeleteRoof_Click(object sender, RoutedEventArgs e)
        {
            lroof.Remove(r);
            loadlstRoof();
            txtTagCeiling.Text = "";
            EnableRoof(false);
            if (lroof.Count == 0)
            {
                iRoof = 1;
            }
        }

        private void lstRoof_GotFocus(object sender, RoutedEventArgs e)
        {
            SaveRoof();
        }

        public void SaveRoof()
        {
            if (r != null)
            {
                if (txtTagCeiling.Text != "")
                {
                    r.sRoofName = txtTagCeiling.Text;
                }
                r.iRoofType = cbxRoofType.SelectedIndex;
                r.dbRoofTransferCoefficient = Convert.ToDouble(txtRoofHeatTransferCoefficient.Text);
                r.dbRoofLength = Convert.ToDouble(txtRoofLength.Text);
                r.dbRoofWidth = Convert.ToDouble(txtRoofWidth.Text);
                r.dbRoofTemperatureExcess = Convert.ToDouble(txtRoofTempExcess.Text);
            }
        }

        public void loadRoof(List<Roof> lr)
        {
            lroof = lr;
            loadlstRoof();
            if (lroof.Count == 0)
            {
                EnableRoof(false);
            }
        }

        public void loadlstRoof()
        {
            lstRoof.ItemsSource = null;
            lstRoof.Items.Clear();
            lstRoof.ItemsSource = lroof;
            lstRoof.DisplayMemberPath = "sRoofName";
        }

        public void EnableRoof(bool b)
        {
            cbxRoofType.IsEnabled = b;
            txtRoofHeatTransferCoefficient.IsEnabled = b;
            txtRoofLength.IsEnabled = b;
            txtRoofWidth.IsEnabled = b;
            txtRoofTempExcess.IsEnabled = b;
        }

        private void lstRoof_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            EnableRoof(true);
            r = (Roof)lstRoof.SelectedItem;
            if (r != null)
            {
                txtTagCeiling.Text = r.sRoofName;
                cbxRoofType.SelectedIndex = r.iRoofType;
                txtRoofLength.Text = r.dbRoofLength.ToString();
                txtRoofWidth.Text = r.dbRoofWidth.ToString();
                txtRoofTempExcess.Text = r.dbRoofTemperatureExcess.ToString();
                txtRoofHeatTransferCoefficient.Text = r.dbRoofTransferCoefficient.ToString();
            }
        }
        #endregion

        #region PROCESS FLOOR
        public void SaveFloor()
        {
            f.dbFloorLength = Convert.ToDouble(txtFloorLength.Text);
            f.dbFloorWidth = Convert.ToDouble(txtFloorWidth.Text);
            f.dbFloorAverageTemperature = Convert.ToDouble(txtFloorAverageTemp.Text);
            f.dbFloorHeatTransferCoefficient = Convert.ToDouble(txtFloorHeatTransferCoefficient.Text);
        }

        public void loadFloor(Floor floor)
        {
            if (f != null)
            {
                AppConstruction();
            }
            f = floor;
            txtFloorLength.Text = f.dbFloorLength.ToString();
            txtFloorWidth.Text = f.dbFloorWidth.ToString();
            txtFloorAverageTemp.Text = f.dbFloorAverageTemperature.ToString();
            txtFloorHeatTransferCoefficient.Text = f.dbFloorHeatTransferCoefficient.ToString();
        }
        #endregion

        private void btnApplyConstruction_Click(object sender, RoutedEventArgs e)
        {
            AppConstruction();
        }

        public void AppConstruction()
        {
            if (lstWall.Items.Count != 0)
            {
                SaveWall();
            }
            if (lstRoof.Items.Count != 0)
            {
                SaveRoof();
            }
            SaveFloor();
            btnApplyConstruction.IsEnabled = false;
        }

        #region CHECK EMPTY
        public void CheckEmpty(TextBox txt)
        {
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        private void txtWallHeatTransferCoefficient_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWallHeatTransferCoefficient);
        }

        private void txtWallOutsideExposure_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWallOutsideExposure);
        }

        private void txtWallTempExcess_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWallTempExcess);
        }

        private void txtWallLength_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWallLength);
        }

        private void txtWallWidth_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWallWidth);
        }

        private void txtRoofHeatTransferCoefficient_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtRoofHeatTransferCoefficient.Text == "")
            {
                txtRoofHeatTransferCoefficient.Text = "0.2";
            }
            if (txtRoofHeatTransferCoefficient.Text.Contains(' '))
            {
                txtRoofHeatTransferCoefficient.Text = txtRoofHeatTransferCoefficient.Text.Remove(txtRoofHeatTransferCoefficient.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txtRoofHeatTransferCoefficient.Text.First() == '.')
            {
                txtRoofHeatTransferCoefficient.Text = txtRoofHeatTransferCoefficient.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        private void txtRoofLength_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtRoofLength);
        }

        private void txtRoofWidth_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtRoofWidth);
        }

        private void txtRoofTempExcess_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtRoofTempExcess);
        }

        private void txtFloorLength_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtFloorLength);
        }

        private void txtFloorWidth_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtFloorWidth);
        }

        private void txtFloorAverageTemp_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtFloorAverageTemp);
        }

        private void txtFloorHeatTransferCoefficient_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtFloorHeatTransferCoefficient.Text == "")
            {
                txtFloorHeatTransferCoefficient.Text = "0.21";
            }
            if (txtFloorHeatTransferCoefficient.Text.Contains(' '))
            {
                txtFloorHeatTransferCoefficient.Text = txtFloorHeatTransferCoefficient.Text.Remove(txtFloorHeatTransferCoefficient.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txtFloorHeatTransferCoefficient.Text.First() == '.')
            {
                txtFloorHeatTransferCoefficient.Text = txtFloorHeatTransferCoefficient.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        private void txtTagWall_TextChanged(object sender, TextChangedEventArgs e)
        {
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtTagCeiling_TextChanged(object sender, TextChangedEventArgs e)
        {
            btnApplyConstruction.IsEnabled = true;
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
                btnApplyConstruction.IsEnabled = true;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                        btnApplyConstruction.IsEnabled = true;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion

        #region GOT FOCUS
        private void txtInput_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            key = txt.Name;
        }

        private void txtInput_Loaded(object sender, RoutedEventArgs e)
        {
            ToolTip tt = new ToolTip();
            TextBox txt = (TextBox)sender;
            ToolTipService.SetPlacement(tt, PlacementMode.Left);
            tt.Content = "Press F1 for Help";
            txt.ToolTip = tt;
        }
        #endregion
    }
}
